package com.server.Service;

import com.server.entity.Login;
import com.server.entity.Users;
import com.server.mapper.LoginMapper;
import org.apache.ibatis.session.SqlSession;
import untils.MybatisClass;
import untils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * The type Login service.
 *
 * @author user
 */
public class LoginService {

    /**
     * Select all array list.
     *
     * @return the array list
     */
    public static ArrayList<Login> selectAll(){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        LoginMapper moviesTypes = sqlSession.getMapper(LoginMapper.class);
        ArrayList<Login> moviesTypesList = (ArrayList<Login>) moviesTypes.selectByExample(null);
        sqlSession.close();
        /*return JSON.toJSONString(moviesList);*/
        return moviesTypesList;
    }

    /**
     * Select by primary key login.
     *
     * @param id the id
     * @return the login
     */
    public static Login selectByPrimaryKey(Integer id){
        SqlSession sqlSession = MybatisClass.getSqlSession();
        LoginMapper moviesTypes = sqlSession.getMapper(LoginMapper.class);
        Login moviesTypesList = moviesTypes.selectByPrimaryKey(id);
        sqlSession.close();
        /*return JSON.toJSONString(moviesList);*/
        return moviesTypesList;
    }

    /**
     * Instr vales int.
     *
     * @param number    the number
     * @param startTime the start time
     * @param entire    the entire
     * @return the int
     */
    public static int instrVales(int number, String startTime, String entire){
        int sumCount = 0;
        SqlSession sqlSession=MybatisClass.getSqlSession();
        LoginMapper loginMapper = sqlSession.getMapper(LoginMapper.class);
        List<Users> users = UserService.getAllTypes();
        for (int i = 0; i < number; i++) {
            sumCount+=loginMapper.insert(new Login(
                    Utils.getRandomString(12)+i,
                    Utils.getRandomString(12)+i,
                    Utils.randomDate(startTime, entire),
                    users.get(new Random().nextInt(users.size())).getUserId()
            ));
            sqlSession.commit();
        }
        sqlSession.close();
        return sumCount;
    }
}
